<?php

namespace app\adminapi\middleware;

use think\Request;
use think\facade\Queue;

class OperateLog
{
    public function handle(Request $request, \Closure $next)
    {
        $response = $next($request);

        $logData = [
            'admin_id' => $request->userInfo['user_id'],
            'ip' => $request->ip(),
            'real_name' => $request->rule()->getOption('real_name') ?: '',
            'url' => $request->url(),
            'method' => $request->method(),
            'params' => $request->param(),
            'route' => $request->rule()->getRule(),
            'code' => $response->getCode()
        ];

        // 异步写入队列
        Queue::push(\app\common\job\OperateLogJob::class, $logData);

        return $response;
    }
}